package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * 游记评论实现类
 */
@Service
public class TravelCommentServiceImpl implements ITravelCommentService {


    @Autowired
    private TravelCommentRepository travelCommentRepository;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(TravelComment travelComment) {
        // 因为Id和userInfo的Id同名 也会被覆盖  需要给他设个null值  让芒果DB 自动生成
        travelComment.setId(null);

        // 根据传上来的travelComment  拿到他的getRefComment() 属性 对象  在拿到这个Id
        String id = travelComment.getRefComment().getId();
        // 如果这个Id 有值 在用get 方法 去拿RefComment() 这个对象中的对象
        if (StringUtils.hasLength(id)){
            TravelComment comment = this.get(id);
            // 把这个对象设置回这个游记评论对象的RefComment()属性中去
            travelComment.setRefComment(comment);
            // 评论中的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }


        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {

        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);

    }

    @Override
    public TravelComment get(String id) {
        Optional<TravelComment> op = travelCommentRepository.findById(id);
        if (op.isPresent()){
            TravelComment travelComment = op.get();
            return travelComment;
        }
        return null;
    }

    @Override
    public List<TravelComment> list()
    {
        return travelCommentRepository.findAll();
    }

    // 分页操作
    @Override
    public List<TravelComment> query(TravelCommentQuery qo) {

        //
       List<TravelComment> list = travelCommentRepository.findByTravelId(qo.getTravelId());


        return list;

    }



}
